<?php
#用户实时定位表
class LocationModel extends PdoModel {
    protected $_name = 'location';

    function __construct(){
        parent::__construct();
    }

    #添加
    function addData($data){
        if(empty($data)){
            return false;
        }
        return $this->insert($data);
    }

    #删除
    function deleteData($id = 0){
        if(empty($id)){
            return false;
        }
        $row = $this->find("id = {$id}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("id = {$id}");
    }

    #更新
    function updateDataById($data,$id){
        return $this->update($data,"id = {$id}");
    }

    function getDataById($id){
        if(empty($id)){
            return array();
        }

        $sql = " select * from {$this->_name} where id={$id} limit 1";
        $result = $this->fetchRow($sql);

        return $result;
    }

    #查找轨迹
    #基本表信息
    function getListByCondition($condition = array()){
        $sql = " select * from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid = {$condition['uid']} ";
        }
        if(!empty($condition['friend_id'])) {
            $sql .= " and friend_id = {$condition['friend_id']} ";
        }
        if(isset($condition['start_time'])) {
            $sql .= " and created_at >= {$condition['start_time']} ";
        }

        if(isset($condition['end_time'])) {
            $sql .= " and created_at <= {$condition['end_time']} ";
        }

        $sql .= " order by id asc ";
        $data = $this->fetchAll($sql);
        return $data;
    }

    #基本表信息
    function getListByAdminCount($condition = array()){
        $sql = " select count(*) as num from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid={$condition['uid']} ";
        }
        if(!empty($condition['friend_id'])) {
            $sql .= " and friend_id = {$condition['friend_id']} ";
        }
        if(isset($condition['isnew'])) {
            $sql .= " and isnew = {$condition['isnew']} ";
        }
        if(isset($condition['type'])) {
            $sql .= " and type in( {$condition['type']} ) ";
        }

        if(isset($condition['page_size'])) {
            $sql .= " limit {$condition['page_size']} ";
        }

        $result = $this->fetchRow($sql);
        $num = 0;
        if(!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }



}
